//
//  InformationCategaryScrollView.m
//  pte
//
//  Created by zk on 16/5/24.
//  Copyright © 2016年 陈无忌. All rights reserved.
//

#import "InformationCategaryScrollView.h"
#define  unselectedColor [UIColor colorWithRed:74.0 / 255.0 green:74.0 / 255.0 blue:74.0 / 255.0 alpha:1]
#define  selectedColor [UIColor colorWithRed:141.0 / 255.0 green:202.0 / 255.0 blue:255.0 / 255.0 alpha:1]
#define BUTTONWIDTH 100

@implementation InformationCategaryScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)ConfigCategaryWithDatasource:(NSMutableArray *)Datasource
{
    self.contentSize = CGSizeMake(BUTTONWIDTH * (Datasource.count) , -10);
    self.backgroundColor = [UIColor whiteColor];
    for (NSInteger i = 0; i < Datasource.count; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(BUTTONWIDTH * i, 0, BUTTONWIDTH, self.frame.size.height - 2)];
        if (i == 0) {
            [button setTitleColor:selectedColor forState:UIControlStateNormal];
            self.selectedButton = button;
        } else {
            [button setTitleColor:unselectedColor forState:UIControlStateNormal];
        }


        [button setTitle:Datasource[i][@"cate_name"] forState:UIControlStateNormal];
            
        button.tag = i;
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    [self addSubview:self.sliderView];
}

- (void)buttonClick:(id)sender {
    UIButton *button = (UIButton *)sender;
    [self.cateDelegate buttonClickWithCateId:button.tag];
    [button setTitleColor:selectedColor forState:UIControlStateNormal];
    if (self.selectedButton == button) {
        
    } else {
        [self.selectedButton setTitleColor:unselectedColor forState:UIControlStateNormal];
    }
    self.selectedButton = button;
    [UIView beginAnimations:@"move" context:nil];
    self.sliderView.frame = CGRectMake(BUTTONWIDTH * button.tag, self.frame.size.height - 2, BUTTONWIDTH, 2);
    [UIView setAnimationDuration:0.5];
    [UIView commitAnimations];
}

- (UIView *)sliderView {
    if (!_sliderView) {
        _sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 2, BUTTONWIDTH, 2)];
        _sliderView.backgroundColor = selectedColor;
        [self addSubview:_sliderView];
    }
    return _sliderView;
}

@end
